/*---------------------------------------------------------------------------
    $Workfile		: AndroidDotNetSettings.h $

	Author			: Joseph Chacko, Ajith Joseph
    Last $Modtime	: 16/11/2011 
    Last $Revision	: 1 $
	Remarks			:
    $History		: AndroidDotNetSettings.h $
 ---------------------------------------------------------------------------*/

#pragma once
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;

#include "NewAndroidProject.h"

namespace AndroidDotNET 
{
	/// <summary>
	/// Summary for AndroidDotNetSettingsForm
	///
	/// WARNING: If you change the name of this class, you will need to change the
	///          'Resource File Name' property for the managed resource compiler tool
	///          associated with all .resx files this class depends on.  Otherwise,
	///          the designers will not be able to interact properly with localized
	///          resources associated with this form.
	/// </summary>
	public ref class AndroidDotNetSettingsForm : public System::Windows::Forms::Form
	{
		CAndroidDotNetSettings^ m_AndroidDotNetSettings;
	public:
		AndroidDotNetSettingsForm(CAndroidDotNetSettings^ SDKSettings)
		{
			m_AndroidDotNetSettings=SDKSettings;
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}
	public:
	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~AndroidDotNetSettingsForm()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::Label^  label1;
	private: System::Windows::Forms::TextBox^  textBoxSDKLocation;
	private: System::Windows::Forms::ListView^  listView1;
	private: System::Windows::Forms::Button^  buttonBrowse;
	private: System::Windows::Forms::Button^  buttonApplay;
	private: System::Windows::Forms::Button^  OK;
	private: System::Windows::Forms::Button^  buttonCancel;
	private: System::Windows::Forms::ColumnHeader^  TargetName;
	private: System::Windows::Forms::ColumnHeader^  Vendors;
	private: System::Windows::Forms::ColumnHeader^  Platform;
	private: System::Windows::Forms::ColumnHeader^  API;
	private: System::Windows::Forms::Label^  label2;

	protected: 

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			System::ComponentModel::ComponentResourceManager^  resources = (gcnew System::ComponentModel::ComponentResourceManager(AndroidDotNetSettingsForm::typeid));
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->textBoxSDKLocation = (gcnew System::Windows::Forms::TextBox());
			this->listView1 = (gcnew System::Windows::Forms::ListView());
			this->TargetName = (gcnew System::Windows::Forms::ColumnHeader());
			this->Vendors = (gcnew System::Windows::Forms::ColumnHeader());
			this->Platform = (gcnew System::Windows::Forms::ColumnHeader());
			this->API = (gcnew System::Windows::Forms::ColumnHeader());
			this->buttonBrowse = (gcnew System::Windows::Forms::Button());
			this->buttonApplay = (gcnew System::Windows::Forms::Button());
			this->OK = (gcnew System::Windows::Forms::Button());
			this->buttonCancel = (gcnew System::Windows::Forms::Button());
			this->label2 = (gcnew System::Windows::Forms::Label());
			this->SuspendLayout();
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Location = System::Drawing::Point(15, 19);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(73, 13);
			this->label1->TabIndex = 0;
			this->label1->Text = L"SDK Location";
			// 
			// textBoxSDKLocation
			// 
			this->textBoxSDKLocation->Location = System::Drawing::Point(94, 16);
			this->textBoxSDKLocation->Name = L"textBoxSDKLocation";
			this->textBoxSDKLocation->Size = System::Drawing::Size(261, 20);
			this->textBoxSDKLocation->TabIndex = 1;
			// 
			// listView1
			// 
			this->listView1->CheckBoxes = true;
			this->listView1->Columns->AddRange(gcnew cli::array< System::Windows::Forms::ColumnHeader^  >(4) {this->TargetName, this->Vendors, 
				this->Platform, this->API});
			this->listView1->Location = System::Drawing::Point(18, 78);
			this->listView1->Name = L"listView1";
			this->listView1->Size = System::Drawing::Size(337, 237);
			this->listView1->TabIndex = 2;
			this->listView1->UseCompatibleStateImageBehavior = false;
			this->listView1->View = System::Windows::Forms::View::Details;
			this->listView1->ItemChecked += gcnew System::Windows::Forms::ItemCheckedEventHandler(this, &AndroidDotNetSettingsForm::listView1_ItemChecked);
			this->listView1->SelectedIndexChanged += gcnew System::EventHandler(this, &AndroidDotNetSettingsForm::listView1_SelectedIndexChanged_1);
			// 
			// TargetName
			// 
			this->TargetName->Text = L"TargetName";
			this->TargetName->Width = 144;
			// 
			// Vendors
			// 
			this->Vendors->Text = L"Platform";
			this->Vendors->Width = 85;
			// 
			// Platform
			// 
			this->Platform->Text = L"API Level";
			this->Platform->Width = 96;
			// 
			// API
			// 
			this->API->Text = L"API---";
			this->API->Width = 0;
			// 
			// buttonBrowse
			// 
			this->buttonBrowse->Location = System::Drawing::Point(280, 39);
			this->buttonBrowse->Name = L"buttonBrowse";
			this->buttonBrowse->Size = System::Drawing::Size(75, 23);
			this->buttonBrowse->TabIndex = 3;
			this->buttonBrowse->Text = L"Browse";
			this->buttonBrowse->UseVisualStyleBackColor = true;
			this->buttonBrowse->Click += gcnew System::EventHandler(this, &AndroidDotNetSettingsForm::buttonBrowse_Click);
			// 
			// buttonApplay
			// 
			this->buttonApplay->Location = System::Drawing::Point(124, 320);
			this->buttonApplay->Name = L"buttonApplay";
			this->buttonApplay->Size = System::Drawing::Size(75, 23);
			this->buttonApplay->TabIndex = 4;
			this->buttonApplay->Text = L"Apply";
			this->buttonApplay->UseVisualStyleBackColor = true;
			this->buttonApplay->Click += gcnew System::EventHandler(this, &AndroidDotNetSettingsForm::buttonApplay_Click);
			// 
			// OK
			// 
			this->OK->Location = System::Drawing::Point(202, 320);
			this->OK->Name = L"OK";
			this->OK->Size = System::Drawing::Size(75, 23);
			this->OK->TabIndex = 5;
			this->OK->Text = L"OK";
			this->OK->UseVisualStyleBackColor = true;
			this->OK->Click += gcnew System::EventHandler(this, &AndroidDotNetSettingsForm::OK_Click);
			// 
			// buttonCancel
			// 
			this->buttonCancel->Location = System::Drawing::Point(280, 320);
			this->buttonCancel->Name = L"buttonCancel";
			this->buttonCancel->Size = System::Drawing::Size(75, 23);
			this->buttonCancel->TabIndex = 6;
			this->buttonCancel->Text = L"Cancel";
			this->buttonCancel->UseVisualStyleBackColor = true;
			this->buttonCancel->Click += gcnew System::EventHandler(this, &AndroidDotNetSettingsForm::buttonCancel_Click);
			// 
			// label2
			// 
			this->label2->AutoSize = true;
			this->label2->Location = System::Drawing::Point(15, 58);
			this->label2->Name = L"label2";
			this->label2->Size = System::Drawing::Size(122, 13);
			this->label2->TabIndex = 7;
			this->label2->Text = L"Select a default Platform";
			// 
			// AndroidDotNetSettingsForm
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(369, 354);
			this->Controls->Add(this->label2);
			this->Controls->Add(this->buttonCancel);
			this->Controls->Add(this->OK);
			this->Controls->Add(this->buttonApplay);
			this->Controls->Add(this->buttonBrowse);
			this->Controls->Add(this->listView1);
			this->Controls->Add(this->textBoxSDKLocation);
			this->Controls->Add(this->label1);
			this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::FixedSingle;
			this->Icon = (cli::safe_cast<System::Drawing::Icon^  >(resources->GetObject(L"$this.Icon")));
			this->MaximizeBox = false;
			this->MinimizeBox = false;
			this->Name = L"AndroidDotNetSettingsForm";
			this->StartPosition = System::Windows::Forms::FormStartPosition::CenterParent;
			this->Text = L"AndroidStudio Settings";
			this->Load += gcnew System::EventHandler(this, &AndroidDotNetSettingsForm::AndroidDotNetSettingsForm_Load);
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
	private: System::Void AndroidDotNetSettingsForm_Load(System::Object^  sender, System::EventArgs^  e)
			 {
				 try
				 {
				 AndroidTargetSettings^ m_AndroidTargetSettings=gcnew AndroidTargetSettings();
				 textBoxSDKLocation->Text=LoadSDKLocationFromRegistry();
				 if(textBoxSDKLocation->Text!="")
				 {
					 //Loading previous target from Registry
					 System::String^ reg_strTargetName=LoadTargetFromRegistry();
					 m_AndroidTargetSettings->LoadAllAndroidTargetPlatforms(textBoxSDKLocation->Text);
					 listView1->Items->Clear();
					 for(int i=0;i<m_AndroidTargetSettings->m_NewAndroidTSArray->Count;i++)
					 {
						 ListViewItem^ Item=listView1->Items->Add(m_AndroidTargetSettings->m_NewAndroidTSArray[i]->m_strTargetName);
						 Item->SubItems->Add(m_AndroidTargetSettings->m_NewAndroidTSArray[i]->m_strPlatform);
						 Item->SubItems->Add(m_AndroidTargetSettings->m_NewAndroidTSArray[i]->m_strAPI);
						 if(reg_strTargetName==m_AndroidTargetSettings->m_NewAndroidTSArray[i]->m_strFolderName)
						 {
							 Item->Checked=true;
						 }
					 }
				 }
				 }
				 catch(Exception^ ex)
				 {
					 MessageBox::Show(ex->Message);
				 }
			 }
private: System::Void listBox1_SelectedIndexChanged(System::Object^  sender, System::EventArgs^  e) {
			 }
	private: System::Void listBox1_SelectedIndexChanged_1(System::Object^  sender, System::EventArgs^  e) {
			 }
	private: System::Void listView1_SelectedIndexChanged(System::Object^  sender, System::EventArgs^  e) {
			 }
private: System::Void listView1_SelectedIndexChanged_1(System::Object^  sender, System::EventArgs^  e) 
		 {	
		 }
private: System::Void buttonApplay_Click(System::Object^  sender, System::EventArgs^  e)
		 {
			 m_AndroidDotNetSettings->m_strSDKLocation=textBoxSDKLocation->Text;
			 SaveSDKLocationToRegistry(textBoxSDKLocation->Text);
			 SaveTargetToRegistry(strNewTarget);
		 }
private: System::Void OK_Click(System::Object^  sender, System::EventArgs^ e)
		 {
			 m_AndroidDotNetSettings->m_strSDKLocation=textBoxSDKLocation->Text;
			 SaveSDKLocationToRegistry(textBoxSDKLocation->Text);
			 SaveTargetToRegistry(strNewTarget);
			 this->Close();
			 DialogResult=System::Windows::Forms::DialogResult::OK;
		 }
 public: CAndroidDotNetSettings^ GetAndroidDotNetSettings()
		 {
			 return m_AndroidDotNetSettings;
		 }
private: System::Void buttonCancel_Click(System::Object^  sender, System::EventArgs^  e) 
		 {
			 this->Close();
		 }
private: System::Void buttonBrowse_Click(System::Object^  sender, System::EventArgs^  e) 
		 {
			 AndroidTargetSettings^ m_AndroidTargetSettings=gcnew AndroidTargetSettings();
			 FolderBrowserDialog^ ObjectBrowser=gcnew  FolderBrowserDialog();
			 ObjectBrowser->ShowDialog();
			 System::String^ m_ObjectPath;
			 m_ObjectPath=ObjectBrowser->SelectedPath;

			 //code to check selected path is a valid SDK folder
			 if(m_ObjectPath!="")
			 {
				 array<System::String^>^ SubDirs= Directory::GetDirectories(m_ObjectPath);
				 System::Boolean flag=false;
				 for(int i=0;i<SubDirs->Length;++i)
				 {
					 if( SubDirs[i]->Contains("platforms"))
					 {
						 flag=true;
					 }
				 }
				 if(flag==false)
				 {
					 System::Windows::Forms::MessageBox::Show("Not a valid SDK DIRECTRY","Error");
				 }
				 else 
				 {
					 textBoxSDKLocation->Text=m_ObjectPath ;
					 m_AndroidTargetSettings->LoadAllAndroidTargetPlatforms(textBoxSDKLocation->Text);
					 listView1->Items->Clear();
					 for(int i=0;i<m_AndroidTargetSettings->m_NewAndroidTSArray->Count;i++)
					 {
						 ListViewItem^ Item=listView1->Items->Add(m_AndroidTargetSettings->m_NewAndroidTSArray[i]->m_strTargetName);
						 Item->SubItems->Add(m_AndroidTargetSettings->m_NewAndroidTSArray[i]->m_strPlatform);
						 Item->SubItems->Add(m_AndroidTargetSettings->m_NewAndroidTSArray[i]->m_strAPI);
						 if(m_AndroidTargetSettings->m_NewAndroidTSArray[i]->m_strTargetName==strNewTarget)
						 {
							 Item->Checked=true;
						 }
					 }
				 }
			 }
		 }
public : static System::String^ strNewTarget="";

private: System::Void listView1_ItemChecked(System::Object^  sender, System::Windows::Forms::ItemCheckedEventArgs^  e) 
		 {
			 AndroidTargetSettings^ m_AndroidTargetSettings=gcnew AndroidTargetSettings();
			 m_AndroidTargetSettings->LoadAllAndroidTargetPlatforms(textBoxSDKLocation->Text);
			 if(e->Item->Checked)
			 {
				 int iChekedIndex=e->Item->Index;
				 for(int i=0;i<listView1->Items->Count;i++)
				 {
					 if(iChekedIndex!=i)
					 {
						listView1->Items[i]->Checked=false;
					 }
					 else
					 {
						 strNewTarget= m_AndroidTargetSettings->m_NewAndroidTSArray[iChekedIndex]->m_strFolderName;
					 }
				 }
			 }	
		 }

//save SDK location to registry		 
private: System::Boolean SaveSDKLocationToRegistry(System::String^ strSDKLocation)
        {
			if(strSDKLocation[strSDKLocation->Length-1]!='\\')
			{
				strSDKLocation+="\\";
			}

			try
            {
				RegistryKey^ key = Registry::CurrentUser->CreateSubKey("Software\\Tek-Tips.Info\\Android\\");
				key->SetValue("DEFAULT_SDK_LOCATION", strSDKLocation);
				key->Close();
				return true;
			}
            catch (...)
            {
                return false;
            }
        }

//Retrive previous SDK location from registry
private: System::String^ LoadSDKLocationFromRegistry()
		 {
			 try
			 {
				 System::String^ strSDKLocation;
				 RegistryKey^ key = Registry::CurrentUser->CreateSubKey("Software\\Tek-Tips.Info\\Android\\");
				 strSDKLocation= key->GetValue("DEFAULT_SDK_LOCATION")->ToString();
				 key->Close();
				 return strSDKLocation;
			 }
			 catch (...)
			 {
				 return "";
			 }
		 }

//retrive previous target from restry		 		 
private: System::String^ LoadTargetFromRegistry()
		 {
			 try
			 {
				 System::String^ strTarget;
				 RegistryKey^ key = Registry::CurrentUser->CreateSubKey("Software\\Tek-Tips.Info\\Android\\");
				 strTarget= key->GetValue("DEFAULT_TARGET_VERSION")->ToString();
				 key->Close();
				 return strTarget;
			 }
			 catch (...)
			 {
				 return "";
			 }
		 }

//save target to registry		 
private: System::Boolean SaveTargetToRegistry(System::String^ strTarget)
        {
            try
            {

			RegistryKey^ key = Registry::CurrentUser->CreateSubKey("Software\\Tek-Tips.Info\\Android\\");
               key->SetValue("DEFAULT_TARGET_VERSION", strTarget);
			   key->Close();
               return true;
            }
            catch (...)
            {
                return false;
            }
        }
};
}
